<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MaterialBill extends BaseModel
{
    use HasFactory;
    //物料清单表
    protected $table = 'material_bill';
    // 允许批量操作的字段
    protected $fillable = [
        'name',
        'product_id',
        'model_number_id',
        'specification_id',
        'color_id',
        'is_del',
        'amount_total',
        'created_at',
        'updated_at',
    ];

    public function scopeSearch($query,$params)
    {
        if(!empty($params['product_id'])){
            $query->where('product_id',$params['product_id']);
        }
        if(!empty($params['model_number_id'])){
            $query->where('model_number_id',$params['model_number_id']);
        }
        if(!empty($params['specification_id'])){
            $query->where('specification_id',$params['specification_id']);
        }
        if(!empty($params['color_id'])){
            $query->where('color_id',$params['color_id']);
        }
        if(!empty($params['material_id'])){
            $query->where('material_id',$params['material_id']);
        }
        return $query;
    }

    public function product(){
        return $this->hasOne(Product::class,'id','product_id');
    }
    public function model_number(){
        return $this->hasOne(ModelNumber::class,'id','model_number_id');
    }
    public function specification(){
        return $this->hasOne(Specification::class,'id','specification_id');
    }
    public function color(){
        return $this->hasOne(Color::class,'id','color_id');
    }

    public function child(){
        return $this->hasMany(MaterialBillChild::class,'bill_id','id');
    }

    public function warehouse(){
        return $this->hasOne(Warehouse::class,'id','warehouse_id');
    }



}
